package com.javatian.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.github.pagehelper.PageInfo;
import com.javatian.common.Result;
import com.javatian.entity.Account;
import com.javatian.entity.VideoInfo;
import com.javatian.service.VideoInfoService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 新能源汽车视频控制器
 */
@RestController
@RequestMapping(value = "/videoInfo")
public class VideoInfoController {

    @Resource
    private VideoInfoService videoInfoService;

    //用户信息
    @Value("${const.user}")
    private String user;

    /**
     * 新增一个新能源汽车视频
     */
    @PostMapping
    public Result<VideoInfo> add(@RequestBody VideoInfo videoInfo, HttpServletRequest request){
        Account account =(Account) request.getSession().getAttribute(user);
        videoInfo.setUserName(account.getName());
        videoInfo.setLevel(account.getLevel());
        videoInfo.setUploadUserId(account.getId());
        videoInfoService.add(videoInfo);
        return Result.success(videoInfo);
    }

    /**
     * 根据主键id删除一个新能源汽车视频
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id, HttpServletRequest request){
        Account account =(Account) request.getSession().getAttribute(user);
        VideoInfo info = videoInfoService.findById(id);
        if (!account.getLevel().equals(info.getLevel())||!account.getId().equals(info.getUploadUserId())){
            return Result.error("1001","不能删除他人的记录");
        }
        videoInfoService.delete(id);
        return Result.success();
    }

    /**
     * 修改一个新能源汽车视频
     */
    @PutMapping
    public Result update(@RequestBody VideoInfo videoInfo,HttpServletRequest request){
        Account account =(Account) request.getSession().getAttribute(user);
        if (!account.getLevel().equals(videoInfo.getLevel())||!account.getId().equals(videoInfo.getUploadUserId())){
            return Result.error("1001","不能修改他人的记录");
        }
        videoInfoService.update(videoInfo);
        return Result.success();
    }

    /**
     * 根据主键id获取一个新能源汽车视频对象
     */
    @GetMapping("/{id}")
    public Result<VideoInfo> detail(@PathVariable Long id){
        VideoInfo videoInfo = videoInfoService.findById(id);
        return Result.success(videoInfo);
    }

    /**
     * 查询所有新能源汽车视频记录
     */
    @GetMapping
    public Result<List<VideoInfo>> all(){
        return Result.success(videoInfoService.findAll());
    }

    /**
     * 根据条件分页查询
     * @param name mc
     * @param pageNum 第几页
     * @param pageSize 每页数量
     * @return 当前页的List列表
     */
    @GetMapping("/page/{name}")
    public Result<PageInfo<VideoInfo>> page(@PathVariable String name,
                                            @RequestParam(defaultValue = "1") Integer pageNum,
                                            @RequestParam(defaultValue = "5") Integer pageSize){
        return Result.success(videoInfoService.findPage(name, pageNum, pageSize));
    }





}
